<?php
/**
 * User: @lixu
 * Date: 2019-02-18
 * Time: 15:30
 */

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\ImgUrl as tImgUrl;

class PinTuan extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'pintuan';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    const TYPE_NEW = 1;
    const TYPE_GMV = 2;

    const TYPE_DESC = [
        self::TYPE_NEW => '新人团',
        self::TYPE_GMV => 'GMV团',
    ];

    public static function getList($where, $field = '*')
    {
       $list = self::select($field, $where);
       foreach ($list as &$info) {
           self::formatInfo($info);
       }
       return $list;
    }

    public static function getInfo($where, $field = '*')
    {
        $info = self::get($field, $where);
        self::formatInfo($info);
        return $info;
    }

    public static function add($data)
    {
        if(self::insert($data)->rowCount()) {
            return self::id();
        }

        return false;
    }

    public static function save($id, $data)
    {
        if(self::update($data, ['id' => $id])->rowCount()) {
            return $id;
        }

        return false;
    }


    public static function getNotOverInfosByGid(array $goodsIds)
    {
        $infos = self::select('*', [
            'AND' => [
                'goods_id' => $goodsIds,
                'end_time[>]' => date('Y-m-d H:i:s'),
                'status' => self::STATUS_NORMAL,
            ],
            'ORDER' => ['end_time' => 'ASC']
        ]);
        $rst = [];
        foreach ($infos as $info) {
            if (!isset($rst[$info['goods_id']])) {
                $rst[$info['goods_id']] = $info;
            }
        }
        return $rst;
    }

    public static function getListByIds($ids)
    {
        return self::select("*", [
            'id' => $ids,
            'status' => self::STATUS_NORMAL
        ]);
    }

    public static function otherGoodsInfos($goodsId, $limit = 5)
    {
        $infos = self::select('*',
            [
                'AND' => [
                    'goods_id[!]'=>$goodsId,
                    'start_time[<]' => date('Y-m-d H:i:s'),
                    'end_time[>]' => date('Y-m-d H:i:s'),
                    'status' => self::STATUS_NORMAL,
                ],
                'LIMIT' => [0, $limit],
                'ORDER' => ['end_time' => 'DESC']
            ]);

        return array_column($infos, null, 'goods_id');
    }

    public static function getCount($where)
    {
        return self::count('*', $where['AND']);
    }

    public static function formatInfo(&$info)
    {

        if (empty($info)) {
            return;
        }
        foreach ($info as $key => $value) {
            switch ($key) {
                case 'cover_img':
                    $info['cover_img_url'] = tImgUrl::getUrl(tImgUrl::APPTYPE_OP, $value, tImgUrl::IMAGE_SIZE_OP_ORI);
                    break;
                case 'type':
                    $info['type_desc'] = self::TYPE_DESC[$value];
                    break;
            }
        }
    }
}